﻿using Application.ISale_hService;
using Domain.Entity.ord;
using Domain.Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Application.Purchase_hService;
using Microsoft.Extensions.Caching.Distributed;
using System.Text.Json;
using Domain.Entity.search;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using WMSManage.Filter;

namespace WMSManage.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]//需要鉴权
    public class Sale_hController : ControllerBase
    {
        private readonly ISale_hService sale_HService;
        private readonly IDistributedCache distributedCache;

        public Sale_hController(ISale_hService sale_HService, IDistributedCache distributedCache)
        {
            this.sale_HService = sale_HService;
            this.distributedCache = distributedCache;
        }


        /// <summary>
        /// 分页查询销售单
        /// </summary>
        /// <param name="code"></param>
        /// <param name="status"></param>
        /// <param name="cust"></param>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
        /// <param name="CurrentPage"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FenYe<Sale_h>> GetSale_h(string? code, int? status, int? cust, string? starttime, string? endtime, int CurrentPage)
        {
            FenYe<Sale_h> fenYe = new FenYe<Sale_h>()
            {
                CurrentPage = CurrentPage,
            };
            return await sale_HService.GetSale_h(fenYe, code, status, cust, starttime, endtime);
        }

        /// <summary>
        /// 高级查询
        /// </summary>
        /// <param name="code"></param>
        /// <param name="starttotal"></param>
        /// <param name="endtotal"></param>
        /// <param name="startdate"></param>
        /// <param name="enddate"></param>
        /// <param name="startnum"></param>
        /// <param name="endnum"></param>
        /// <param name="status"></param>
        /// <param name="cust"></param>
        /// <param name="custlink"></param>
        /// <param name="dep"></param>
        /// <param name="user"></param>
        /// <param name="createname"></param>
        /// <param name="startcreatetime"></param>
        /// <param name="endcreatetime"></param>
        /// <param name="approvername"></param>
        /// <param name="startapprovertime"></param>
        /// <param name="endapprovertime"></param>
        /// <param name="CurrentPage"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FenYe<Sale_h>> GetAdvancedSale_h(string? code, string? starttotal, string? endtotal, string? startdate, string? enddate, string? startnum, string? endnum, int? status, int? cust, string? custlink, int? dep, int? user, string? createname, string? startcreatetime, string? endcreatetime, string? approvername, string? startapprovertime, string? endapprovertime, int CurrentPage)
        {
            FenYe<Sale_h> fenYe = new FenYe<Sale_h>()
            {
                CurrentPage = CurrentPage,
            };
            return await sale_HService.GetAdvancedSale_h(fenYe, code, starttotal, endtotal, startdate, enddate, startnum, endnum, status, cust, custlink, dep, user, createname, startcreatetime, endcreatetime, approvername, startapprovertime, endapprovertime);
        }



        /// <summary>
        /// 添加销售单
        /// </summary>
        /// <param name="addSale_HDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddSale_h(AddSale_hDto addSale_HDto)
        {
            return await sale_HService.AddSale_h(addSale_HDto);
        }


        /// <summary>
        /// 根据id查询销售单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Sale_h> GetSale_HById(int id)
        {
            return await sale_HService.GetSale_HById(id);
        }



        /// <summary>
        /// 修改销售单
        /// </summary>
        /// <param name="updateSale_HDto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateSale_h(UpdateSale_hDto updateSale_HDto)
        {
            return await sale_HService.UpdateSale_h(updateSale_HDto);
        }




        /// <summary>
        /// 删除销售单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> DeleteSale_h(int id)
        {
            return await sale_HService.DeleteSale_h(id);
        }



        /// <summary>
        /// 审核销售单
        /// </summary>
        /// <param name="saleid"></param>
        /// <param name="status"></param>
        /// <param name="approvername"></param>
        /// <param name="approvertime"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> ApproverSale_h(int saleid, int status, string approvername, string approvertime)
        {
            return await sale_HService.ApproverSale_h(saleid, status, approvername, approvertime);
        }



        /// <summary>
        /// 修改单据状态
        /// </summary>
        /// <param name="saleid"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateStatus(int saleid, int status)
        {
            return await sale_HService.UpdateStatus(saleid, status);
        }


        /// <summary>
        /// 查询所有销售单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Sale_h>> GetSale_Hs()
        {
            return await sale_HService.GetSale_Hs();
        }



        /// <summary>
        /// 根据货品id查询销售记录
        /// </summary>
        /// <param name="procid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Sale_h>> GetAllSale_hBypid(int procid)
        {
            return await sale_HService.GetAllSale_hBypid(procid);
        }


        /// <summary>
        /// 销售统计
        /// </summary>
        /// <param name="timeType"></param>
        /// <param name="code"></param>
        /// <param name="cust"></param>
        /// <param name="user"></param>
        /// <param name="dep"></param>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<SaleTJResult> Sale_hTJ(string? timeType, string? code, int? cust, int? user, int? dep, string? starttime, string? endtime, int? page, int? pagesize)
        {
            //缓存键
            string key = "SaleTJResult" + timeType + code + cust + user + dep + starttime + endtime + page + pagesize;
            //从缓存中获取数据
            var data = await distributedCache.GetStringAsync(key);
            if (data != null)
            {
                //从缓存中查
                return JsonSerializer.Deserialize<SaleTJResult>(data);
            }
            //从数据库查
            var result = await sale_HService.Sale_hTJ(timeType, code, cust, user, dep, starttime, endtime, page, pagesize);
            //将查询到的结果存入缓存
            if (result != null)
            {
                var option = new DistributedCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10)//10分钟
                };
                await distributedCache.SetStringAsync(key, JsonSerializer.Serialize(result), option);
            }
            return result;
        }


        /// <summary>
        /// 销售统计刷新
        /// </summary>
        /// <param name="timeType"></param>
        /// <param name="code"></param>
        /// <param name="cust"></param>
        /// <param name="user"></param>
        /// <param name="dep"></param>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Refresh(string? timeType, string? code, int? cust, int? user, int? dep, string? starttime, string? endtime, int? page, int? pagesize)
        {
            //缓存键
            string key = "SaleTJResult" + timeType + code + cust + user + dep + starttime + endtime + page + pagesize;

            // 清除缓存键
            distributedCache.Remove(key);

            // 返回成功消息
            return Ok(new { success = true, message = "ok" });
        }




        /// <summary>
        /// 更新已退货数量及状态
        /// </summary>
        /// <param name="saleid"></param>
        /// <param name="returnnum"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPut]
        public Task<int> UpdateRNum(int saleid, int returnnum, int status,int newnum)
        {
            return sale_HService.UpdateRNum(saleid, returnnum, status,newnum);
        }
    }
}
